package com.hp.ppm.ext.kuka;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.config.PropertiesConfig;
import com.config.PropertiesService;
import com.hp.ppm.ext.service.UserCache;
import com.utils.DbBuilder;
import org.apache.commons.io.FileUtils;

public class RequestTypeQuery {
	
	PropertiesConfig config = PropertiesService.getApplicationConfig();
	String tasksql;
	String showsql;

	public RequestTypeQuery() throws IOException {
		tasksql= FileUtils.readFileToString( new File( config.getProperty("sql.file")),"UTF-8");
        showsql= FileUtils.readFileToString( new File( config.getProperty("show.sql")),"UTF-8");
	}

	public String getRequestTypeJson(String username){

		List<RequestTypeMessage> tasklist= getRequestTypeByUser(username);
        List<RequestTypeMessage> showlist= getRequestTypeByUserShow(username);

        List<RequestTypeMessage> alllist=new ArrayList<>(  );

		for (RequestTypeMessage requestTypeMessage : tasklist) {
			String gotourl=config.getProperty("request.url")+requestTypeMessage.getRequestId();
			if(UserCache.getInstancei().get(username)!=null){
				gotourl+="&mname="+username+"&mpwd="+UserCache.getInstancei().get(username);
			}
			requestTypeMessage.setRequestUrld(gotourl);
			requestTypeMessage.setMessageType( RequestTypeMessage.LINK_MESSAGE );
            alllist.add( requestTypeMessage );
		}

        for (RequestTypeMessage requestTypeMessage : showlist) {
            requestTypeMessage.setMessageType(  RequestTypeMessage.SHOW_MESSAGE);
            requestTypeMessage.setRequestUrld( "#" );
            alllist.add( requestTypeMessage );
        }

        return JSON.toJSONString(alllist);
	}


	
	public List<RequestTypeMessage> getRequestTypeByUser(String username){
		String sql=tasksql.replace( "?",username );
		System.out.println(sql);
		List<RequestTypeMessage> beanListResult = DbBuilder.getListBean(sql, RequestTypeMessage.class);
		return beanListResult;
	}

    public List<RequestTypeMessage> getRequestTypeByUserShow(String username){
        String sql=showsql.replace( "?",username );
        System.out.println(sql);
        List<RequestTypeMessage> beanListResult = DbBuilder.getListBean(sql, RequestTypeMessage.class);
        return beanListResult;
    }



    public static void main(String[] args) throws IOException {
		RequestTypeQuery query=new RequestTypeQuery();
		System.out.println(query.getRequestTypeJson("zhoukun"));
		
	}	
}
